import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';

const app = getApp();

Page({
    data: {
        member: 0
    },
    async onReady() {
        const db = qq.cloud.database();
        const _ = db.command;
        let This = this;
        await db.collection("sign-in").where({
                [app.data.act]: _.neq('挂机'),
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                This.setData({ member: res.data });
            });
        await db.collection("controlSign-in").where({
                [app.data.act]: _.neq('挂机'),
                alliance: app.data.alliance
            })
            .get()
            .then(res => {
                let member = This.data.member;
                res.data.map((value) => {
                    value.control = true;
                    member.push(value);
                })
                This.setData({ member: member });
            });
    },
    async delete(e) {
        await Dialog.confirm({
                title: '提示',
                message: '请确认该成员是否挂机',
            })
            .then(async() => {
                const db = qq.cloud.database();
                const _ = db.command;
                let admin = false;
                const This = this;
                await db.collection('administer').where({
                    unionId: _.eq(app.data.unionId)
                }).get().then((res) => {
                    if (res.data.length != 0) {
                        admin = true
                    }
                })
                if (admin) {
                    if (!e.currentTarget.dataset.control) {
                        db.collection("sign-in")
                            .where({
                                unionId: _.eq(e.target.dataset.id),
                                alliance: app.data.alliance,
                                userName: _.eq(e.target.dataset.username),
                            })
                            .update({
                                data: {
                                    [app.data.act]: _.set("挂机")
                                }
                            })
                            .then(async(res) => {
                                console.log(res);
                                Toast('修改成功');
                                await db.collection("sign-in").where({
                                        [app.data.act]: _.neq('挂机'),
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        This.setData({ member: res.data });
                                    });
                                await db.collection("controlSign-in").where({
                                        [app.data.act]: _.neq('挂机'),
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(res => {
                                        let member = This.data.member;
                                        res.data.map((value) => {
                                            value.control = true;
                                            member.push(value);
                                        })
                                        This.setData({ member: member });
                                    });
                            })
                            .catch(err => {
                                Toast('修改失败');
                            })
                    } else {
                        db.collection("controlSign-in")
                            .where({
                                unionId: _.eq(e.target.dataset.id),
                                userName: _.eq(e.target.dataset.username),
                                alliance: app.data.alliance
                            })
                            .update({
                                data: {
                                    [app.data.act]: _.set("挂机")
                                }
                            })
                            .then(res => {
                                Toast('修改成功');
                                db.collection("controlSign-in").where({
                                        [app.data.act]: _.neq('挂机'),
                                        alliance: app.data.alliance
                                    })
                                    .get()
                                    .then(async(res) => {
                                        This.setData({ member: res.data });
                                        await db.collection("sign-in").where({
                                                [app.data.act]: _.neq('挂机'),
                                                alliance: app.data.alliance
                                            })
                                            .get()
                                            .then(res => {
                                                This.setData({ member: res.data });
                                            });
                                        await db.collection("controlSign-in").where({
                                                [app.data.act]: _.neq('挂机'),
                                                alliance: app.data.alliance
                                            })
                                            .get()
                                            .then(res => {
                                                let member = This.data.member;
                                                res.data.map((value) => {
                                                    value.control = true;
                                                    member.push(value);
                                                })
                                                This.setData({ member: member });
                                            });
                                    });
                            })
                            .catch(err => {
                                Toast('修改失败');
                            })
                    }
                } else {
                    Toast('没有管理员权限');
                }
            })
            .catch(() => {
                // on cancel
            });

    },
    onShareAppMessage() {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        });
    },


})